package com.mdd.maduoduo.activity;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mdd.maduoduo.R;
import com.mdd.maduoduo.adapter.AlreadyReceiveAdapter;
import com.mdd.maduoduo.adapter.MainListAdapter;
import com.mdd.maduoduo.di.component.DaggerUIComponent;
import com.mdd.maduoduo.di.component.UIComponent;
import com.mdd.maduoduo.entity.BannerEntity;
import com.mdd.maduoduo.entity.QueryListEntity;
import com.mdd.maduoduo.entity.Response;
import com.mdd.maduoduo.entity.StationEntity;
import com.mdd.maduoduo.presenter.LoginPresenter;
import com.mdd.maduoduo.utils.AlertUtils;
import com.mdd.maduoduo.utils.CommonAdapter;
import com.mdd.maduoduo.utils.DpUtils;
import com.mdd.maduoduo.utils.GlideUtils;
import com.mdd.maduoduo.utils.ToastUtils;
import com.mdd.maduoduo.utils.Utils;
import com.mdd.maduoduo.view.interfaces.DeleteCodeView;
import com.mdd.maduoduo.view.interfaces.GetBannerView;
import com.mdd.maduoduo.view.interfaces.GetStationView;
import com.mdd.maduoduo.view.interfaces.QueryListView;
import com.mdd.maduoduo.view.interfaces.UpdateCodeStatusView;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.config.IndicatorConfig;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.listener.OnBannerListener;

import java.util.List;

import javax.inject.Inject;

public class AlreadyReceiveActivity extends BaseActivity implements GetStationView, QueryListView, UpdateCodeStatusView, AlreadyReceiveAdapter.UpdateStatusListener, GetBannerView, DeleteCodeView {

    private ImageView ivBack;

    @Inject
    LoginPresenter mPresenter;

    private SmartRefreshLayout refreshLayout;

    private LinearLayout llView;

    private int selectPos = 0;

    private StationEntity mStationEntity;

    private int page= 1;

    private AlreadyReceiveAdapter mAdapter;

    private RecyclerView recyclerView;

    //0-所有、1-今天、2-昨天、3-本周、4-本月
    private int pickupTimeType = 0;
    
    private LinearLayout llDate;

    private Banner banner;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_already_receive;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.onDestroy();
    }

    @Override
    protected void initializeInjector() {
        super.initializeInjector();
        mPresenter = new LoginPresenter();
        UIComponent uiComponent = DaggerUIComponent.builder().applicationComponent(getApplicationComponent())
                .activityModule(getActivityModule()).build();
        uiComponent.inject(this);
        mPresenter.setGetStationView(this);
        mPresenter.setQueryListView(this);
        mPresenter.setUpdateCodeStatusView(this);
        mPresenter.setGetBannerView(this);
        mPresenter.setDeleteCodeView(this);
    }


    @Override
    protected void initView() {
        super.initView();
        ivBack = findViewById(R.id.ivBack);
        refreshLayout = findViewById(R.id.refreshLayout);
        llView = findViewById(R.id.llView);
        llDate = findViewById(R.id.llDate);
        recyclerView = findViewById(R.id.recyclerView);
        banner = findViewById(R.id.banner);
        mAdapter = new AlreadyReceiveAdapter();
        recyclerView.setAdapter(mAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter.setListener(this);
        mAdapter.setOnItemLongClickListener(new CommonAdapter.OnItemLongClickListener<QueryListEntity.DataDTO.RecordsDTO>() {
            @Override
            public boolean onItemLongClick(CommonAdapter<QueryListEntity.DataDTO.RecordsDTO> adapter, View view, int position, QueryListEntity.DataDTO.RecordsDTO item) {
                AlertUtils.normalBtnAlert(AlreadyReceiveActivity.this, "温馨提示","确定要删除该取件码吗？","取消","确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mPresenter.deleteCode(item.id,0);
                        AlertUtils.dismiss();
                    }
                });
                return false;
            }
        });
        //添加日期选择
        addDateSelect();
        mPresenter.getStation(this,1);
        mPresenter.getBannerList("2002");
    }

    private void addDateSelect() {
        llDate.removeAllViews();
        for (int i = 0; i < 5; i++) {
            TextView textView = new TextView(this);
            switch (i){
                case 0:
                    textView.setText("全部");
                    break;
                case 1:
                    textView.setText("今天");
                    break;
                case 2:
                    textView.setText("昨天");
                    break;
                case 3:
                    textView.setText("本周");
                    break;
                case 4:
                    textView.setText("本月");
                    break;
            }

            if (pickupTimeType ==i){
                textView.setBackgroundResource(R.drawable.shape_radius_200_color_000000);
                textView.setTextColor(Color.parseColor("#FFFFFF"));
            }else {
                textView.setBackgroundResource(R.drawable.shape_radius_200_color_ffffff);
                textView.setTextColor(Color.parseColor("#000000"));
            }

            textView.setPadding(DpUtils.dip2px(this,12),DpUtils.dip2px(this,5),DpUtils.dip2px(this,12),DpUtils.dip2px(this,5));
            int finalI = i;
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    pickupTimeType = finalI;
                    page = 1;
                    addDateSelect();
                    refreshLayout.autoRefresh();
                }
            });
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT
            );
            params.setMargins(10, 0, 10, 0);
            llDate.addView(textView, params);
        }
    }

    @Override
    protected void onClick() {
        ivBack.setOnClickListener(view -> finish());
    }

    @SuppressLint("SetTextI18n")
    private void refreshUi(StationEntity stationEntity) {
        llView.removeAllViews();
        for (int i = 0; i < stationEntity.data.records.size(); i++) {
            TextView textView = new TextView(this);
            textView.setText(stationEntity.data.records.get(i).stationName+" "+stationEntity.data.records.get(i).statusNumber);
            if (selectPos==i){
                textView.setBackgroundResource(R.drawable.shape_radius_200_color_000000);
                textView.setTextColor(Color.parseColor("#FFFFFF"));
            }else {
                textView.setBackgroundResource(R.drawable.shape_radius_200_color_ffffff);
                textView.setTextColor(Color.parseColor("#000000"));
            }

            textView.setPadding(DpUtils.dip2px(this,12),DpUtils.dip2px(this,5),DpUtils.dip2px(this,12),DpUtils.dip2px(this,5));
            int finalI = i;
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    selectPos = finalI;
                    refreshUi(mStationEntity);
                    page = 1;
                    refreshLayout.autoRefresh();
                }
            });
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT
            );
            params.setMargins(10, 0, 10, 0);
            llView.addView(textView, params);
        }
//        refreshLayout.autoRefresh();
        if (!mStationEntity.data.records.isEmpty()){
            mPresenter.queryList(this,page,10,1,mStationEntity.data.records.get(selectPos).stationName,pickupTimeType);
        }

    }

    @Override
    public void renderSuccess(StationEntity stationEntity) {
        mStationEntity = stationEntity;
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout rs) {
                if (!mStationEntity.data.records.isEmpty()){
                    page = 1;
                    mPresenter.queryList(AlreadyReceiveActivity.this,page,10,1,mStationEntity.data.records.get(selectPos).stationName,pickupTimeType);
                }
                refreshLayout.finishRefresh(2000);
            }
        });

        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout rs) {
                if (!mStationEntity.data.records.isEmpty()){
                    page ++;
                    mPresenter.queryList(AlreadyReceiveActivity.this,page,10,1,mStationEntity.data.records.get(selectPos).stationName,pickupTimeType);
                }
                refreshLayout.finishLoadMore(2000);
            }
        });
        refreshUi(mStationEntity);
    }

    @Override
    public void renderSuccess(QueryListEntity queryListEntity) {
        if (page==1){
            mAdapter.setData(queryListEntity.data.records);
        }else {
            mAdapter.addData(queryListEntity.data.records);
        }
        if (queryListEntity.data.pages == page){
            refreshLayout.finishLoadMoreWithNoMoreData();
        }else {
            refreshLayout.setNoMoreData(false);  // 逆转无更多数据状态
        }
    }

    @Override
    public void renderSuccess(Response response) {
        if (response.code.equals("200")){
            ToastUtils.showLongToast(this,"撤回取件成功");
            page = 1;
            selectPos = 0;
            mPresenter.getStation(this,1);
            refreshLayout.autoRefresh();
        }else {
            ToastUtils.showLongToast(this,response.msg);
        }
    }

    @Override
    public void update(QueryListEntity.DataDTO.RecordsDTO data) {
        //撤回需要二次确认
        String content = "确定要将该包裹回退到代取件状态吗?";
        AlertUtils.normalBtnAlert(this, "温馨提示",content,"取消","确定", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPresenter.updateCodeStatus(AlreadyReceiveActivity.this,data.id,0,0,"");
                AlertUtils.dismiss();
            }
        });
    }

    @Override
    public void renderBannerSuccess(BannerEntity bannerEntity) {
        List<BannerEntity.DataDTO.FunctionConfigDtosDTO> listBanner = bannerEntity.data.functionConfigDtos;
        if (!listBanner.get(0).itemDtos.isEmpty()){
            banner.setVisibility(View.VISIBLE);
            //加载banner数据
            banner.setAdapter(new BannerImageAdapter<BannerEntity.DataDTO.FunctionConfigDtosDTO.ItemDtosDTO>(listBanner.get(0).itemDtos) {
                @Override
                public void onBindView(BannerImageHolder holder, BannerEntity.DataDTO.FunctionConfigDtosDTO.ItemDtosDTO data, int position, int size) {
                    GlideUtils.getInstances().loadNormalImg(AlreadyReceiveActivity.this,holder.imageView,data.url);
                }
            });
            banner.setOnBannerListener(new OnBannerListener() {
                @Override
                public void OnBannerClick(Object data, int position) {
                    //根据jumpType判断跳转位置 点击跳转类型:0-跳转网页、1-跳转五星好评、2-执行分享功能、3-跳转打赏界面、4-跳转Max会员购买界面、6-跳转小程序、7-客服电话(有拨打效果)、8-仅展示(点击可复制)、9-跳转多人共享界面
                    BannerEntity.DataDTO.FunctionConfigDtosDTO.ItemDtosDTO bean = (BannerEntity.DataDTO.FunctionConfigDtosDTO.ItemDtosDTO) data;
                    Utils.jumpOther(AlreadyReceiveActivity.this,bean);
                }
            });
            // 设置指示器
            banner.setIndicator(new CircleIndicator(AlreadyReceiveActivity.this));
            // 设置指示器位置
            banner.setIndicatorGravity(IndicatorConfig.Direction.CENTER);
            banner.start();
        }else {
            banner.setVisibility(View.GONE);
        }
    }

    @Override
    public void renderDeleteSuccess(Response response) {
        if (response.code.equals("200")){
            ToastUtils.showLongToast(AlreadyReceiveActivity.this,"删除成功");
            page = 1;
            selectPos = 0;
            mPresenter.getStation(AlreadyReceiveActivity.this,1);
        }
    }
}
